import * as Types from '../constant/ActionTypes';
import request from '../utils/request';
import { message } from 'antd';

// 登陆
export const login = payload => async (dispatch) => {
  const { data } = await request({
    url: '/api/login',
    method: 'post',
    params: payload,
  });
  message.success('登陆成功', 1);
  dispatch(saveAuth(data));
  return data;
};

// 保存用户信息
export const saveAuth = (payload = {}) => ({
  type: Types.USER_LOGIN,
  payload,
});

// 清空信息
export const clearAuth = () => ({
  type: Types.CLEAR_AUTH,
});

// 注销登陆
export const logout = () => async (dispatch) => {
  await request({
    url: '/api/logout',
  });
  message.success('已注销登陆', 1);
  dispatch(clearAuth());
};
